package com.software;
import java.util.Arrays;
public class Demo3 {
    /**
     * 使用冒泡排序、插入排序对整型数组数据进行排序
     * @param args
     */
    public static void main(String[] args) {
        int[] arr = new int[]{85,6,2,78,7,4,12,5,22,1,46,0};
        int[] arr2 = Arrays.copyOf(arr,arr.length);
        System.out.println("打印排序前的数组arr：");
        for (int i : arr) {
            System.out.print(i + " ");
        }
        for(int i = 0; i < arr.length - 1; i++){
            int temp;
            for(int j = 0; j < arr.length - 1 - i; j++){
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println();
        System.out.println("打印冒泡排序后的数组：");
        for(int i : arr){
            System.out.print(i + " ");
        }
        System.out.println();
        System.out.println("打印插入排序前的数组arr2：");
        for(int i : arr2){
            System.out.print(i + " ");
        }
        System.out.println();
        for(int i = 0; i < arr2.length - 1; i++){
            int temp;
            int j = i;
            while(j >= 0 && arr2[j] > arr2[j + 1]){
                temp = arr2[j];
                arr2[j] = arr2[j + 1];
                arr2[j + 1] = temp;
                j--;
            }
        }
        for (int i = 0; i < arr2.length - 1; i++) {
            int temp;
            for(int j = i; j>= 0 && arr2[j] > arr2[j+1]; j--){
//                temp = arr2[j];
//                arr2[j] = arr2[j + 1];
//                arr2[j + 1] = temp;
                arr2[j] = arr2[j] ^ arr2[j + 1];
                arr2[j + 1] = arr2[j] ^ arr2[j + 1];
                arr2[j] = arr2[j] ^ arr2[j + 1];
            }
        }
        System.out.println("插入排序后的数组");
        for (int i : arr2) {
            System.out.print(i + " ");
        }

    }
}
